package cvs;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;

public class CSVGeneratorMulti {

	public static char comma = ',';
	public static char quotation = '"';

	public static char r = '\r';
	public static char n = '\n';
	public static String line_separator = System.getProperty("line.separator");

	protected String fileName;

	public CSVGeneratorMulti(String fileName) {
		this.fileName = fileName;
	}

	public static String fileToString(String file) {

		FileReader fileReader;
		StringBuffer sql = new StringBuffer();
		try {
			fileReader = new FileReader(file);
			BufferedReader reader = new BufferedReader(fileReader);

			String line = null;
			while ((line = reader.readLine()) != null) {
				sql.append(line);
				sql.append("\n");
			}

		} catch (Exception e) {
			e.printStackTrace();
		}

		return sql.toString();
	}

	private BufferedWriter getWriter(ResultSetMetaData rsmd, int columns,
			int index) throws SQLException, IOException {
		BufferedWriter out = null;

		String newFile = fileName + "_" + index + ".csv";
		try {
			out = new BufferedWriter(new FileWriter(newFile), 4096);
			System.out.println("new File : " + newFile);
		} catch (IOException e) {
			e.printStackTrace();
		}

		// print header
		boolean is_first = true;
		for (int i = 1; i <= columns; i++) {
			String value = changeToCSV(rsmd.getColumnLabel(i));

			if (is_first)
				out.write(value);
			else
				out.write("," + value);
			is_first = false;
		}
		out.write("\n");

		return out;
	}

	public void build(ResultSet rs) throws SQLException, IOException {
		ResultSetMetaData rsmd = rs.getMetaData();
		int columns = rsmd.getColumnCount();

		BufferedWriter out = null;

		int fileId = 0;
		int index = 0;
		while (rs.next()) {

			if (index % 50000 == 0) {
				++fileId;
				closeOut(out);
				out = getWriter(rsmd, columns, fileId);
			}
			++index;

			try {
				boolean is_first = true;
				for (int i = 1; i <= columns; i++) {
					String value = changeToCSV(rs.getString(i));

					if (is_first)
						out.write(value);
					else
						out.write("," + value);
					is_first = false;
				}
				out.write("\r\n");
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		closeOut(out);
	}

	private void closeOut(BufferedWriter out) {
		if (out != null) {
			try {
				out.flush();
				out.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}

	public String changeToCSV(String cell) {
		if (cell == null) {
			return "";
		}

		cell = cell.replace(CSVGeneratorMulti.r, ' ');
		cell = cell.replace(CSVGeneratorMulti.n, ' ');

		if (cell.indexOf(CSVGeneratorMulti.comma) >= 0
				|| cell.indexOf(CSVGeneratorMulti.quotation) >= 0) {
			String newCell = cell.replaceAll("\"", "\"\"");
			return "\"" + newCell + "\"";
		} else
			return cell;
	}

}
